<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Task_model extends CI_Model
{	
	public function __construct()
	{
		parent::__construct();
	}
	
	function create_task($data)
	{
		$insert = $this->db->insert('tasks',$data);		
		return $this->db->insert_id($insert);		
	}
	
	public function get_my_tasks()
	{
		$this->db->select('*');
		$this->db->from('tasks');
		$this->db->join('task_followers', 'tasks.user_id = tasks_follower.user_id');
		$this->db->where('user_id', $this->session->userdata('user_id')); 
		$result = $this->db->result();
		return $result;
		//$this->db->get_where('tasks',array('user_id'=>$this->session->userdata('user_id')))->result();
	}
	
	public function get_completed_tasks()
	{
		return $this->db->get_where('tasks',array('user_id'=>$this->session->userdata('user_id'),'is_completed'=>'1'))->result();
	}
	
	public function get_achieved_tasks()
	{
	return $this->db->get_where('tasks',array('user_id'=>$this->session->userdata('user_id'),'is_achieved'=>'1'))->result();	
	}
	
	public function get_pending_tasks()
	{
	return $this->db->get_where('tasks',array('user_id'=>$this->session->userdata('user_id'),'is_completed'=>'0','is_achieved'=>'0'))->result();	
	}
		
	public function get_task($id)
	{
		$this->db->select('tasks.*,projects.name,projects.assigned_to');
		$this->db->from('tasks');
		$this->db->where('tasks.id ='.$id);
		$this->db->join('projects','tasks.project_id=projects.id');
		return $this->db->get()->row_array();			
	}
	
	public function get_subtask($task_id)
	{
	return $this->db->order_by('is_achieved asc,ordering asc')->get_where('sub_tasks',array('task_id'=>$task_id,'trash'=>0))->result();	
	}
	
	public function get_trashed_subtask($task_id)
	{
		return $this->db->order_by('is_achieved asc,ordering asc')->get_where('sub_tasks',array('task_id'=>$task_id,'trash'=>1))->result();	
	}
	
	/* note function  */
	public function add_note($insert)
	{
		$this->db->insert('task_notes',$insert);
	}
	
	public function get_note_by_id($id)
	{
		$this->db->order_by('id','DESC');
		$this->db->select('task_notes.*,users.id as user_id,users.username as username,users.profile_image as profile_image');
		$this->db->from('task_notes');
		$this->db->where('task_notes.task_id ='.$id);
		$this->db->join('users','task_notes.user_id = users.id');
		//$this->db->get();
		//echo $this->db->last_query();
		//exit;
		return $this->db->get()->result();	
	}
	
	public function delete_note($id)
	{
		if($this->db->delete('task_notes',array('id'=>$id)))
		{
		$this->db->delete('replies',array('note_id'=>$id));
		return true;	
		}
		else { return false ; }
	}
	
	public function get_note($id){
		return $this->db->select('id,note,note_type')->get_where('task_notes',array('id'=>$id))->row();
	}
	
	public function update_note($id,$set){
	$this->db->update('task_notes',$set,array('id'=>$id));
	}	
	
	public function delete_task($id)
	{
		if($this->db->delete('tasks',array('id'=>$id)))
		{
			$this->db->delete('sub_tasks',array('task_id'=>$id));
			return true;	
		}
		else { return false ; }		
	}
	
	public function edit($edit_id)
	{
		$this->db->select('tasks.*,users.username');
		$this->db->from('tasks');
		$this->db->where('tasks.id ='.$edit_id);
		$this->db->join('users', 'tasks.user_id = users.id');
	 return $this->db->get()->row_array();
	}
	
	public function update($update,$edit_id)
	{
		$this->db->update('tasks',$update,array('id'=>$edit_id));
	}
	
	public function is_achieved($update,$task_id)
	{
	$this->db->update('tasks',$update,$task_id);	
	}
	
	public function task_unachieved($update,$task_id)
	{
		$this->db->update('tasks',$update,$task_id);
	}
	
	public function check_is_achieved($task_id)
	{
		$this->db->select('is_achieved');
		$this->db->from('tasks');
		$this->db->where('tasks.id ='.$task_id);
		return $this->db->get()->row_array();
		
	}
	
	public function check_is_completed($task_id)
	{
		$this->db->select('is_completed');
		$this->db->from('tasks');
		$this->db->where('tasks.id ='.$task_id);
		return $this->db->get()->row_array();
	}
	
	public function task_closed($update,$task_id)
	{
	$this->db->update('tasks',$update,$task_id);	
	}
	
	public function task_open($update,$task_id)
	{
		$this->db->update('tasks',$update,$task_id);
	}
	
	/* sub tasks functions */
	
	public function create_subtask($insert)
	{
		$this->db->insert('sub_tasks',$insert);		
	}
	public function create_subtask_heading($insert)
	{
		$this->db->insert('sub_tasks',$insert);
	}
	
	public function edit_subtask($id)
	{
		return $this->db->get_where('sub_tasks',array('id'=>$id))->result();
	}
	
	public function update_subtask($update,$id,$task_id)
	{
		if($this->db->update('sub_tasks',$update,array('id'=>$id)))
		{			
		// after update update all the time in the database		
		//get all the time subtask of that particular tasks
		$this->db->select('*');
		$this->db->from('sub_tasks');
		$this->db->where('sub_tasks.task_id',$task_id);
		$time_spent = $this->db->get()->result();
		$dbtime_tot = 0;
		foreach($time_spent as $ts){			
			$dbtime_explode = explode(':',$ts->time_spent);
		 	$dbtot_hr 		= $dbtime_explode[0]*60*60;
			$dbtot_min		= $dbtime_explode[1]*60;				
			$dbtime_tot 	+= ($dbtot_hr + $dbtot_min);
		}		
		$hours 			= floor($dbtime_tot/ 3600);
		$minutes 		= floor(($dbtime_tot / 60) % 60);			
		$sumtime = $hours .':'.$minutes;		
		$this->db->update('tasks',array('time_spent'=>$sumtime),array('id'=>$task_id));	
		}
	}
	
	public function subtask_achieved($subtask_id)
	{		
		if($this->db->update('sub_tasks',array('is_achieved'=>1),array('id'=>$subtask_id)))
		{
		return 'yes';	
		}
		else { return 'false';}
	}
	
	public function subtask_unachieved($subtask_id)
	{		
		$this->db->update('sub_tasks',array('is_achieved'=>0),array('id'=>$subtask_id));
	}
	
	public function check_subtask_is_achieved($subtask_id)
	{	
		$this->db->cache_on();
		$this->db->select('is_achieved');
		$this->db->from('sub_tasks');
		$this->db->where('sub_tasks.id ='.$subtask_id);
		return $this->db->get()->row_array();
	}
	public function delete_subtask($delete_id)
	{
		$this->db->delete('sub_tasks',array('id'=>$delete_id));
	}
	
	public function move_trash_subtask($subtask_id){
		$this->db->update('sub_tasks',array('trash'=>'1'),array('id'=>$subtask_id));
	}
	
	public function restore_trash_subtask($subtask_id){
		$this->db->update('sub_tasks',array('trash'=>'0'),array('id'=>$subtask_id));
	}
	
	/* Project function starts here */ 
	public function create_project($insert)
	{
		$this->db->insert('projects',$insert);
		return $this->db->insert_id();
	}
	
/*
	* @author : Swarup
*/	
	public function get_all_projects()
	{
		$access  = array('founder','superadmin','admin','manager');		
		if($this->ion_auth->in_group($access)):
		$this->db->select('projects.*,users.username');
		$this->db->from('projects');
		$this->db->order_by('projects.name','ASC');			
		$this->db->join('users', 'projects.created_by = users.id');				
		elseif($this->ion_auth->is_client()):
		$this->db->select('projects.*');
		$this->db->from('projects');
		$this->db->order_by('projects.name','ASC');					
		$this->db->where('projects.client_id',$this->session->userdata('user_id'));
		else:
		$this->db->distinct('tasks.project_id,projects.name');
		$this->db->select('tasks.project_id,projects.*');
		$this->db->from('tasks');
		$this->db->where("tasks.user_id = {$this->session->userdata('user_id')} OR projects.assigned_to = {$this->session->userdata('user_id')} OR projects.client_id ={$this->session->userdata('user_id')} ");		
		$this->db->join('projects','tasks.project_id = projects.id');		
		endif;
			
		return $this->db->get()->result();		
	}
	
	public function get_project_manager($pID){
		$this->db->select('projects.assigned_to');
		$this->db->from('projects');
		$this->db->where('id',$pID);		
		return $this->db->get()->row()->assigned_to;		 
	}
	
	public function my_projects(){	
			
		$this->db->distinct('project_id');
		$this->db->select('project_id');
		$this->db->select('tasks.project_id,projects.name,projects.assigned_to,projects.end_date');
		$this->db->from('tasks');
		$this->db->join('projects','tasks.project_id = projects.id');
		$this->db->where("tasks.user_id",$this->session->userdata('user_id'));
				
		return $this->db->get()->result();		
	
		$this->db->close();
	}
		
	public function remaining_my_projects(){			
	
		$this->db->select('*');
		$this->db->from('projects');
		$this->db->where("projects.assigned_to",$this->session->userdata('user_id'));
				
		return $this->db->get()->result();		

		$this->db->close();
	}
	
	public function shared_projects(){
		$this->db->select('*');
		$this->db->from('project_followers');
		$this->db->where('project_followers.user_id',$this->session->userdata('user_id'));
		$this->db->join('projects','projects.id = project_followers.project_id');
		return $this->db->get()->result();
		$this->db->close();
	}
	
	
	public function my_recent_projects(){			
		$this->db->distinct('tasks.project_id,projects.name');
		$this->db->select('tasks.project_id,projects.*');
		$this->db->from('tasks');
		$this->db->where("tasks.user_id = {$this->session->userdata('user_id')} OR projects.assigned_to = {$this->session->userdata('user_id')}");	
		$this->db->join('projects','tasks.project_id = projects.id');	
		$this->db->limit(5);
		return $this->db->get()->result();	
	}
	
	public function add_fav_project($id){
		$insert = array(
		'project_id' => $id,
		'user_id' => $this->session->userdata('user_id'),
		);
	$this->db->insert('favourites',$insert);
	}
	
	public function get_fav_project($pid,$uid){
		return $this->db->get_where('favourites',array('project_id'=>$pid,'user_id'=>$uid))->row();
	}
	
	public function remove_fav_project($id){	
	$this->db->delete('favourites',array('project_id' => $id));
	}
	
	// fav tasks 
	
	public function add_fav_tasks($id){
		$insert = array(
		'task_id' => $id,
		'user_id' => $this->session->userdata('user_id'),
		);
	$this->db->insert('favourites',$insert);
	}
	
	public function get_fav_tasks($pid,$uid){
		return $this->db->get_where('favourites',array('task_id'=>$pid,'user_id'=>$uid))->row();
	}
	
	public function remove_fav_tasks($id){	
	$this->db->delete('favourites',array('task_id' => $id));
	}
	
	
	public function get_completed_projects()
	{
		return $this->db->get_where('projects',array('is_completed'=>1))->result();
	}
	
	public function get_incompleted_projects()
	{
		return $this->db->get_where('projects',array('is_completed'=>0))->result();
	}
	
	
	public function get_percentage_by_projectid($Pid){
		$cpercentage = $this->db->select('completed_percentage')->get_where('tasks',array('project_id' => $Pid,'is_completed'=>0))->result_array();
		
		if(count($cpercentage)>0){
		$sum =0;		
		foreach($cpercentage as $cp){
			$sum += $cp['completed_percentage'];			
		}		
		return $avg = ($sum / count($cpercentage));		
		}		
	}
	/* Project function end here */ 	
	/* Milestone function end here */ 
	
	public function get_project_name_by_id($project_id)
	{
		return $this->db->get_where('projects',array('id'=>$project_id))->row_array();
	}	
	public function create_milestone($insert)
	{
		$this->db->insert('milestones',$insert);
	}	
	public function get_milestone_by_project_id($project_id)
	{
		$this->db->select('milestones.*,users.username');
		$this->db->from('milestones');
		$this->db->where('project_id ='.$project_id);
		$this->db->join('users', 'milestones.created_by = users.id');		
	 return $this->db->get()->result();
	}
	
	public function get_milestones()
	{		
	return $this->db->get('milestones')->result();
	}
	
	public function get_default_milestone()
	{
		return $this->db->get_where('milestones',array('is_default'=> 1))->result();
	}
	
	public function get_taskgroups()
	{		
	return $this->db->get('task_groups')->result();
	}
	
	public function create_taskgroups($insert)
	{
		$this->db->insert('task_groups',$insert);
	}
	

/*
	* @author : Swarup
*/
	public function get_default_taskgroups()
	{
	$this->db->select('*');
	$this->db->from('task_groups');
	$this->db->where('is_default =',1);
	$this->db->order_by('name','ASC');
	return $this->db->get()->result();
	}
	
	public function get_custom_taskgroups($project_id)
	{
	return $this->db->get_where('task_groups',array('is_default'=>0, 'project_id' => $project_id))->result();
	$this->db->close();
	}
	
	public function get_taskgroup_by_id($project_id,$milestone_id)
	{
	return $this->db->get_where('task_groups',array('project_id'=>$project_id,'milestone_id'=>$milestone_id,'is_default'=>0))->result();		
	}
	
	public function get_tasks_all($project_id,$milestone_id,$taskgroup_id)
	{
		return $this->db->get_where('tasks',array('project_id'=>$project_id,'task_id'=>$task_id,'milestone_id'=>$milestone_id))->result();
	}
	
		public function get_tasks_by_taskgroup($taskgroup_id)
		{
			return $this->db->get_where('tasks',array('taskgroup_id'=>$taskgroup_id))->result();
		}
		
		public function get_tasks_by_taskgroup_and_project($taskgroup_id,$project_id)
		{
			return $this->db->order_by('is_completed,completed_percentage','ASC')->get_where('tasks',array('project_id'=>$project_id,'taskgroup_id'=>$taskgroup_id))->result();
		}
		
		public function sorting_subtask($action,$updateRecordsArray)
		{
			
		if ($action == "updateRecordsListings"){
		
			$listingCounter = 1;
			foreach ($updateRecordsArray as $recordIDValue) {
				
				$this->db->update('sub_tasks',array('ordering'=>$listingCounter),array('id'=>$recordIDValue));		
				$listingCounter = $listingCounter + 1;	
			}			
			print_r($updateRecordsArray);			
			} 
		}
		
		public function completed_percentage($percentage,$id)
		{
			echo $percentage;			
		$this->db->update('tasks',array('completed_percentage'=>$percentage),array('id'=>$id));
		}
		
		/**
		* edit project 
		*/
		public function getProjectDetails($id){
			return $this->db->get_where('projects',array('id'=>$id))->row_array();
		}
		
		public function update_project($update,$id)
		{
			$this->db->update('projects',$update,array('id'=>$id));
		}
		
		public function project_completed($id){			
			$this->db->update('projects',array('is_completed' =>1),array('id'=> $id));
		}
		public function project_incompleted($id){			
			$this->db->update('projects',array('is_completed' =>0),array('id'=> $id));
		}
		public function get_project_by_department($id){
			return $this->db->order_by('ordering')->get_where('projects',array('department_id'=>$id,'is_completed'=>0))->result();
		}
		
		public function sorting_projects($action,$updateRecordsArray)
		{
			
		if ($action == "updateRecordsListings"){
		
			$listingCounter = 1;
			foreach ($updateRecordsArray as $recordIDValue) {
				
				$this->db->update('projects',array('ordering'=>$listingCounter),array('id'=>$recordIDValue));
				$listingCounter = $listingCounter + 1;
		
	
			}		
	
			print_r($updateRecordsArray);		
			} 
		}
		
		public function time_spent($timespent,$id){
			$this->db->update('tasks',array('time_spent'=>$timespent),array('id'=>$id));
		}
		public function auto_time_update($id){
			//get all the time for that particular user
			return $this->db->get_where('sub_tasks',array('task_id'=>$id))->result();			
		}
		
		public function get_clients(){
			$this->db->select('client_details.*,users.*');
			$this->db->from('client_details');
			$this->db->join('users','client_details.client_id = users.id');
			return $this->db->get()->result();
		}
		
		// get subtasks for worksheet
		public function	get_subtask_for_worksheet($id){
			return $this->db->get_where('sub_tasks',array('id'=>$id))->row_array();				
		}
			
		public function get_task_project($id){
		$this->db->select('tasks.title,projects.name,projects.id');		
		$this->db->from('tasks');
		$this->db->where('tasks.id',$id);
		$this->db->join('projects','tasks.project_id = projects.id');
		return $this->db->get()->row_array();
		}
		
		
		public function add_worksheet($data,$id){
			
			$check = array(
			'year_id' 	=>$data['year_id'],
			'month_id'	=>$data['month_id'],
			'user_id' 	=>$data['user_id'],
			'date_id '	=>$data['date_id'],
			'desc'		=>$data['desc'],
			);
			
			$num_rows = $this->db->get_where('worksheet',$check)->num_rows();
			if($num_rows == 0)
			{									
			$this->db->insert('worksheet',$data);
			//{				
			/*	$time_explode = explode(':',$data['time_spent']);
		 		$tot_hr 		= $time_explode[0]*60*60;
				$tot_min 		= $time_explode[1]*60;				
				
				$tol_time 		= $tot_hr + $tot_min;			
				// get the timespent on particular subtasks
				$dbtime_spent  = $this->db->query("SELECT TIME_FORMAT(time_spent, '%H:%i') as time_spent FROM sub_tasks WHERE id = {$id}")->row()->time_spent;
				
				$dbtime_explode = explode(':',$dbtime_spent);
		 		$dbtot_hr 		= $dbtime_explode[0]*60*60;
				$dbtot_min		= $dbtime_explode[1]*60;				
				$dbtime_tot 	= $dbtot_hr + $dbtot_min;
				
				$tot_time_spent = $dbtime_tot + $tol_time;				
				$hours 			= floor($tot_time_spent / 3600);
				$minutes 		= floor(($tot_time_spent / 60) % 60);
				
				$sumtime = $hours .':'.$minutes;
				//exit;
				$this->db->update('sub_tasks',array('time_spent'=>$sumtime),array('id'=>$id));*/
				//$this->db->update('worksheet',array('time_spent'=>$sumtime),array('id'=>$id));
				
			}
			else {
				return 'FALSE';
			}
		}
		
		// reply function starts here 
		function add_reply($insert){
			if($this->db->insert('replies',$insert)){
			return TRUE;
			}
			else{
			return FALSE;
			}
		}
		
		public function get_notes_replies($note_id){		
		$this->db->order_by('timestamp','ASC');
		$this->db->select('replies.*,users.username as username,users.profile_image');
		$this->db->from('replies');
		$this->db->where('replies.note_id',$note_id);
		$this->db->join('users','replies.user_id = users.id');
		return $this->db->get()->result();
		}
		
		public function delete_reply($id){
			$this->db->delete('replies',array('id'=>$id));
		}
		
	public function add_feedback($insert){
		$this->db->insert('feedbacks',$insert);
	}
	
	// for project access
	public function tasks_access($userid){
		$this->db->select('id');
		$this->db->from('projects');
		$this->db->where('client_id',$userid);
		$pID = $this->db->get()->result();
		
		foreach($pID as $key => $value ){			
			$this->db->select('id');			
			$this->db->from('tasks');
			$this->db->where('project_id',$value->id);
			$tasks[] =  $this->db->get()->result_array();
		}
		return $tasks;
	}
		
	public function project_access($userid){
		$this->db->select('id');
		$this->db->from('projects');
		$this->db->where('client_id',$userid);
		return $this->db->get()->result_array();
	}
	/**
	* special notes of each subtaks
	* Author: Arpan Pratik Kumar
	*/
	public function get_special_notes($subtask_id){
		
		$this->db->order_by('id','ASC');
		$this->db->select('special_subtask_notes.*,users.id as user_id,users.username as username,users.profile_image as profile_image');
		$this->db->from('special_subtask_notes');
		$this->db->where('special_subtask_notes.subtask_id ='.$subtask_id);
		$this->db->join('users','special_subtask_notes.user_id = users.id');		
		return $this->db->get()->result();		
	}		
	public function get_subtask_by_id($id){
		return $this->db->get_where('sub_tasks',array('id'=>$id))->row_array();
	}
	public function save_discussion($note,$id){
		$insert = array(
			'subtask_id'=>$id,
			'user_id'	=> $this->session->userdata('user_id'),
			'note' 		=>$note,			
		);
		if($this->db->insert('special_subtask_notes',$insert)){
			return TRUE;
		}else{
			return FALSE;
		}
	}
	public function delete_discussion($id){
		if($this->db->delete('special_subtask_notes',array('id'=>$id))){
		echo 'Deleted Succesfully';
		}else {
			echo 'Error in processing';
		}
	}
	
	public function follow_project($id){
		$this->db->insert('project_followers',array('user_id'=>$this->session->userdata('user_id'),'project_id'=>$id));
	}
	
	public function get_task_name($id){
		return $this->db->get_where('tasks',array('id'=>$id))->row()->desc;
	}
	
	public function get_feedbacks($task_id){
		return $this->db->order_by('id','DESC')->get_where('feedbacks',array('task_id'=>$task_id))->result();
	}
	
	public function save_multiple_subtask($insert){		
		$this->db->insert('sub_tasks',$insert);
	}
	public function delete_feedback($id){
		$this->db->delete('feedbacks',array('id'=>$id));
	}
	
	public function feedback_percentage($task_id){
		$this->db->select('emotion');
		$this->db->from('feedbacks');
		$this->db->where('task_id',$task_id);
		$this->db->where('emotion','happy');
		$happpy = $this->db->count_all_results();
		
		
		// sad
		$this->db->select('emotion');
		$this->db->from('feedbacks');
		$this->db->where('task_id',$task_id);
		$this->db->where('emotion','sad');
		$sad = $this->db->count_all_results();
		
		//frown
		$this->db->select('emotion');
		$this->db->from('feedbacks');
		$this->db->where('task_id',$task_id);
		$this->db->where('emotion','frown');
		$frown = $this->db->count_all_results();
		
		$return = array(
		'happy' => $happpy,
		'sad' => $sad,
		'frown' => $frown,
		
		);
		if( ($happpy == 0) && ($sad==0) && ($frown==0)){
			return FALSE;
		}else{
		return $return;	
		}
	}
	
	
	/*	
	* @param:task_id
	* @return:array
	* @author : pratik	
	*/
		
	// get all followers for a particular tasks
	public function get_task_followers($task_id){
	$this->db->select('task_followers.*,users.first_name,users.username');
	$this->db->from('task_followers');
	$this->db->where('task_followers.task_id',$task_id);
	$this->db->join('users','task_followers.user_id = users.id');		
	return $this->db->get()->result();
	$this->db->close();
	}
	

	/*
	* @param undefined $followers
	* @param undefined $task_id
	* @author : pratik
*/
	public function add_follower($followers,$task_id){
		foreach($followers as $key => $value){
			$this->db->insert('task_followers',array('user_id'=>$value,'task_id'=>$task_id));
		}
		$this->db->close();
	}
	
	public function remove_follower($id){
		$this->db->delete('task_followers',array('id'=>$id));
		$this->db->close();
	}
	
	public function remove_follower_project($id){
		$this->db->delete('project_followers',array('id'=>$id));
		$this->db->close();
	}
		

 	/* get all followers for the current project */

	public function get_project_followers($project_id){
	$this->db->select('project_followers.*,users.first_name,users.username');
	$this->db->from('project_followers');
	$this->db->where('project_followers.project_id',$project_id);
	$this->db->join('users','project_followers.user_id = users.id');		
	return $this->db->get()->result();
	$this->db->close();
	}
	

	public function add_project_follower($followers,$project_id){		
		foreach($followers as $key => $value){
			$this->db->insert('project_followers',array('user_id'=>$value,'project_id'=>$project_id));
		}
		$this->db->close();		
	}
	
	public function get_custom_task_group($id){
		$this->db->select('id,name,desc,project_id');
		$this->db->from('task_groups');
		$this->db->where('id',$id);
		return $this->db->get()->row_array();		
	}
	
		public function update_custom_task_group($id,$update){
		$this->db->update('task_groups',$update,array('id'=> $id));
		
	}
	
}
